package com.changzhi.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.changzhi.common.constans.RedisConstans;
import com.changzhi.common.exception.ServiceException;
import com.changzhi.common.util.Result;
import com.changzhi.ldyl.model.ActivityPrize;
import com.changzhi.ldyl.model.ActivityWallet;
import com.changzhi.ldyl.model.Invite;
import com.changzhi.mapper.ActivityPrizeMapper;
import com.changzhi.mapper.ActivityWalletMapper;
import com.changzhi.service.*;
import com.changzhi.service.impl.CarnivalServiceImpl;
import com.changzhi.service.impl.InviteServiceImpl;
import com.changzhi.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description:测试
 * @author: 小落
 * @date: 2020年10月10日
 */
@RestController
@RequestMapping("init")
@Slf4j
public class InitController {

	@Autowired
	private OrderDayService orderDayService;
	@Autowired
	private CarnivalService carnivalService;
	@Autowired
	private UserGiftDayService userGiftDayService;
	@Autowired
	private ActivityWalletMapper activityWalletMapper;
	@Autowired
	private SignService signService;
	@Resource
	private ActivityPrizeMapper activityPrizeMapper;
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private InviteService inviteService;

	@PostMapping("inviteGivePrize")
	public Object inviteGivePrize(@RequestBody Map<String,Object> map) {
		checkPwd((String) map.get("pwd"));
		inviteService.inviteGivePrize();
		return "ok";
	}

	@PostMapping("updateInviteTime")
	public Object updateInviteTime(@RequestBody Map<String,Object> map) {
		checkPwd((String) map.get("pwd"));
		redisUtil.del(RedisConstans.INVITE + InviteServiceImpl.INVITE_ID);
		Invite invite = inviteService.getById(InviteServiceImpl.INVITE_ID);
		if(invite == null){
			throw new ServiceException("数据不存在");
		}
		redisUtil.set(RedisConstans.INVITE + InviteServiceImpl.INVITE_ID, invite, RedisConstans.ONE_DAY);
		//存活动时间
		InviteServiceImpl.TIME_MAP.put("startTime", invite.getStartTime());
		InviteServiceImpl.TIME_MAP.put("endTime", invite.getEndTime());
		return "ok";
	}

	@PostMapping("givePrize2")
	public Object givePrize2(@RequestBody Map<String,Object> map) {
		checkPwd((String) map.get("pwd"));
		Integer ACTIVITY_ID = 15;
		LambdaQueryWrapper<ActivityPrize> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(ActivityPrize::getActivityId, ACTIVITY_ID).eq(ActivityPrize::getStatus, 1).ge(ActivityPrize::getType, 10).lt(ActivityPrize::getType, 100);
		List<ActivityPrize> prizeList = activityPrizeMapper.selectList(wrapper);
		int num = 1;
		//8.发放达到要求的奖励
		LambdaQueryWrapper<ActivityWallet> queryWrapper =  new LambdaQueryWrapper<>();
		queryWrapper.eq(ActivityWallet::getActivityId, ACTIVITY_ID);
		List<ActivityWallet> walletList = activityWalletMapper.selectList(queryWrapper);
		for (ActivityWallet wallet : walletList) {
			if(wallet != null){
				Integer value1 = wallet.getAmount1() > wallet.getAmount2() ? wallet.getAmount1() : wallet.getAmount2();
				signService.giveReward(wallet.getUserId(), value1, prizeList);
			}
			log.warn("========:{}, {}" , num, wallet.getUserId());
			num++;
		}

		return "ok";
	}

	/*@PostMapping("givePrize")
	public Object givePrize(@RequestBody Map<String,Object> map) {
		checkPwd((String) map.get("pwd"));
		weekStarService.givePrize();
		return "ok";
	}*/

	@PostMapping("get")
	public Object get(@RequestBody Map<String,Object> map) {
		checkPwd((String) map.get("pwd"));
		//carnivalService.initBeastInfo();
		Map<String, Object> map1 = new ConcurrentHashMap<>();
		map1.put("ID", CarnivalServiceImpl.ID);
		map1.put("BEAST_MAP_size", JSON.toJSON(CarnivalServiceImpl.BEAST_MAP.size()));
		map1.put("HARM_MAP_size", JSON.toJSON(CarnivalServiceImpl.HARM_MAP.size()));
		map1.put("BEAST_MAP", JSON.toJSON(CarnivalServiceImpl.BEAST_MAP));
		map1.put("HARM_MAP", JSON.toJSON(CarnivalServiceImpl.HARM_MAP));
		if(CarnivalServiceImpl.maxHarmUserId != null){
			map1.put("maxHarmUserId", CarnivalServiceImpl.maxHarmUserId);
		}
		return map1;
	}

	@PostMapping("addChannelRobotByFirst")
	public Object lotteryBatch(@RequestBody Map<String,Object> map) {
		checkPwd((String) map.get("pwd"));
		String channelId = (String) map.get("channelId");
		Integer id = (Integer) map.get("id");
		carnivalService.addChannelRobotByFirst(channelId, Long.valueOf(id));
		return "ok";
	}

	@PostMapping("clearChannelReBoot")
	public Object clearChannelReBoot(@RequestBody Map<String,Object> map) {
		checkPwd((String) map.get("pwd"));
		carnivalService.clearChannelReBoot();
		return "ok";
	}

	/*@PostMapping("repairFirstOrder")
	public Object repairFirstOrder(@RequestBody Map<String,Object> map) {
		checkPwd((String) map.get("pwd"));
		orderDayService.repairFirstOrder((String) map.get("startDate"), (String) map.get("endDate"));
		return "ok";
	}*/

	@PostMapping("initBeastInfo")
	public Object initBeastInfo(@RequestBody Map<String,Object> map) {
		//checkPwd((String) map.get("pwd"));
		//carnivalService.initBeastInfo();
		carnivalService.initBeastInfo();
		return "ok";
	}

	@PostMapping("repairOrder")
	public Object repairOrder(@RequestBody Map<String,Object> map) {
		checkPwd((String) map.get("pwd"));
		orderDayService.repairOrder((String) map.get("startDate"), (String) map.get("endDate"));
		return "ok";
	}




	@PostMapping("/repairGiftDay")
	public Object repairGiftDay(@RequestBody Map<String,Object> map){
		checkPwd((String) map.get("pwd"));
		userGiftDayService.repairGiftDay((String) map.get("startDate"), (String) map.get("endDate"));
		return "ojk";
	}

	@PostMapping("/repairFirstInfo")
	public Object repairFirstInfo(@RequestBody Map<String,Object> map){
		checkPwd((String) map.get("pwd"));
		userGiftDayService.repairFirstInfo((String) map.get("startDate"));
		return "ojk";
	}

	private void checkPwd(String pwd){
		if(!"8704".equals(pwd)){
			throw new ServiceException("密码错误！");
		}
	}

	@PostMapping("/updateBossInfo")
	public Object updateBossInfo(@RequestBody Map<String,Object> map){
		checkPwd((String) map.get("pwd"));
		carnivalService.initBeastInfo();
		return Result.success();
	}



}
